<?php
namespace CHOMP\SGIBundle\Form\Type;

use CHOMP\SGIBundle\Entity\Empresa;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormBuilderInterface;

class IncidenteNuevoType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
                $builder->add('titulo');
		$builder->add('estado','entity',array(
                    'class' => 'CHOMP\SGIBundle\Entity\Estado',
//                    'read_only' => true,
                    'disabled' => true)); // Activo disabled porque con read_only no alcanza
		$builder->add('prioridad');
		
		$builder->add('responsableSoporte');
		$builder->add('responsableCliente');
		$builder->add('grupoSoporte');

		
		/*$builder->add('creador');*/
		
		/*$builder->add('fechaCreacion'); 
		$builder->add('fechaModificacion');
		$builder->add('fechaLimiteSLA');
		$builder->add('fechaCierreAutomatico');
		$builder->add('fechaCierre');*/
		
		
		$builder->add('empresa', 'entity', array(
			'class' => 'CHOMP\SGIBundle\Entity\Empresa',
			'empty_value' => '== Seleccione Empresa =='
		));
		$builder->add('cuentaAnalitica', 'choice', array(
			'required'    => false,
			'empty_value' => '== Seleccione Empresa ==',
			'empty_data'  => null
		));

		$builder->add('descripcion','textarea',array(
                        'mapped'=>false));
//                $builder->add('adjunto', 'file',array(
//                        'mapped'=>false));

		$builder->add('save', 'submit',array('label'=>'Grabar'));
		$builder->add('update_data', 'submit');
		
		// Implementando la relación Uno a Muchos:
		// http://symfony.com/doc/current/cookbook/form/form_collections.html
 		 
 		 /*
 		 $builder->add('comentarios', 'collection', array(
			 'type'         => new ComentarioDescripcionType(),
			 'by_reference' => false,					// Utiliza el Método addComentario de Incidente
		 ));
		 */
		
		$formModifier = function(FormInterface $form, $empresa) {
			if ($empresa != null) {
				$form->add('cuentaAnalitica', 'entity',
							array('class' => 'CHOMPSGIBundle:CuentaAnalitica',
								  'property' => 'descripcion',
								  'query_builder' => function($repository) use ($empresa) { 
									return $repository->createQueryBuilder('u')
										->WHERE('u.empresa = :p_empresa')
										->setParameter(':p_empresa', $empresa );
									} , 
							'empty_value' => '== Seleccione Cuenta Analitica ==',
							'empty_data'  => null));
			}
        };

        $builder->addEventListener(
            FormEvents::PRE_SET_DATA,
            function(FormEvent $event) use ($formModifier) {
                // this would be your entity, i.e. Empresa
                $data = $event->getData();

                $formModifier($event->getForm(), $data->getEmpresa());
            }
        );

        $builder->get('empresa')->addEventListener(
            FormEvents::POST_SUBMIT,
            function(FormEvent $event) use ($formModifier) {
                // It's important here to fetch $event->getForm()->getData(), as
                // $event->getData() will get you the client data (that is, the ID)
                $empresa = $event->getForm()->getData();

                // since we've added the listener to the child, we'll have to pass on
                // the parent to the callback functions!
                $formModifier($event->getForm()->getParent(), $empresa);
            }
        );
    }
	
	public function getName()
    {
        return 'chomp_sgibundle_incidente';
    }
}
?>